package org.example.mock;

import com.github.javafaker.Faker;
import org.example.bean.StoreInfo;

import java.sql.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class StoreInfoDataGenerator {
    private static final String DB_URL = "jdbc:mysql://hadoop102/bookstore?useSSL=false&requireSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "000000";
    private static final int BATCH_SIZE = 50;
    private static final Faker faker = new Faker(Locale.CHINA);
    private static final String[] PROVINCES = {"北京", "上海", "广东", "江苏", "浙江", "四川", "湖北"};

    public static void main(String[] args) {
        int count = 1000; // 默认生成20条数据
        if (args.length >= 1) count = Integer.parseInt(args[0]);
        generateData(count);
    }

    public static void generateData(int count) {
        List<StoreInfo> stores = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            LocalDateTime now = LocalDateTime.now();
            String province = PROVINCES[faker.random().nextInt(PROVINCES.length)];

            StoreInfo store = StoreInfo.builder()
                    .storeId("S" + String.format("%04d", faker.number().numberBetween(1000, 9999)))
                    .storeName(province + faker.address().cityName() + "书店")
                    .province(province)
                    .city(province.equals("北京") || province.equals("上海") ? province : province + "省")
                    .district(faker.options().option("东城区", "西城区", "天河区", "黄浦区", "青羊区", "洪山区"))
                    .address(faker.address().streetAddress())
                    .area(java.math.BigDecimal.valueOf(faker.number().randomDouble(2, 5000, 20000)))
                    .openDate(LocalDate.now().minusYears(faker.number().numberBetween(1, 20)))
                    .manager(faker.name().fullName())
                    .employeeCount(faker.number().numberBetween(20, 100))
                    .contactPhone("0" + faker.phoneNumber().subscriberNumber(10))
                    .status(0) // 默认正常状态
                    .createTime(now)
                    .updateTime(now)
                    .build();
            stores.add(store);
        }

        insertData(stores);
    }

    private static void insertData(List<StoreInfo> stores) {
        String sql = "INSERT IGNORE INTO store_info (store_id, store_name, province, city, district, " +
                "address, area, open_date, manager, employee_count, contact_phone, status, " +
                "create_time, update_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            conn.setAutoCommit(false);
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                for (int i = 0; i < stores.size(); i++) {
                    StoreInfo store = stores.get(i);
                    pstmt.setString(1, store.getStoreId());
                    pstmt.setString(2, store.getStoreName());
                    pstmt.setString(3, store.getProvince());
                    pstmt.setString(4, store.getCity());
                    pstmt.setString(5, store.getDistrict());
                    pstmt.setString(6, store.getAddress());
                    pstmt.setBigDecimal(7, store.getArea());
                    pstmt.setDate(8, Date.valueOf(store.getOpenDate()));
                    pstmt.setString(9, store.getManager());
                    pstmt.setInt(10, store.getEmployeeCount());
                    pstmt.setString(11, store.getContactPhone());
                    pstmt.setInt(12, store.getStatus());
                    pstmt.setTimestamp(13, Timestamp.valueOf(store.getCreateTime()));
                    pstmt.setTimestamp(14, Timestamp.valueOf(store.getUpdateTime()));
                    pstmt.addBatch();

                    if ((i + 1) % BATCH_SIZE == 0) pstmt.executeBatch();
                }
                pstmt.executeBatch();
                conn.commit();
                System.out.println("成功插入" + stores.size() + "条店铺数据");
            } catch (SQLException e) {
                conn.rollback();
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}